package com.reder.practice.controller;

import com.reder.practice.common.annotation.FromSession;
import com.reder.practice.bean.User;
import com.reder.practice.common.dto.ServerResponse;
import com.reder.practice.common.enums.state.CommonState;
import com.reder.practice.service.FollowService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/**
 * 关注 API 层
 *
 * @author Red
 * email: 1318944013@qq.com
 * date: 2019/9/5 9:40
 */
@ApiOperation("关注业务 API")
@RestController
@RequestMapping(path = "/follow")
public class FollowController {

    @Resource
    private FollowService followService = null;

    // 返回需要登录响应
    private ServerResponse needLogin() {
        return ServerResponse.response(CommonState.YOU_NEED_LOGIN_FIRST);
    }

    @ApiOperation("关注某个用户")
    @RequestMapping(method = RequestMethod.POST, path = "/{userId}")
    public ServerResponse follow(@ApiParam("要被关注的那个用户 id") @PathVariable long userId,
                                 @ApiIgnore @FromSession User user) {
        if (user == null) {
            return needLogin();
        }
        return followService.follow(user.getId(), userId);
    }

    @ApiOperation("取消关注某个用户")
    @RequestMapping(method = RequestMethod.DELETE, path = "/{userId}")
    public ServerResponse notFollow(@ApiParam("要被取消关注的用户 id（这个倒霉蛋又要掉粉丝了）") @PathVariable long userId,
                                    @ApiIgnore @FromSession User user) {
        if (user == null) {
            return needLogin();
        }
        return followService.notFollow(user.getId(), userId);
    }
}
